#include "plugin.h"

Plugin::Plugin(std::string command)
{
    name = std::string(command);
    std::string setPluginName = "setPluginName(\"";
    setPluginName += name + "\");";
    fp = popen(command.c_str(), "r");
    if(fp == NULL)
    {
        std::cout <<"error opening fp\n";
    }
    webView = new WebWidget();
    webView->open("file:///home/armen/projects/shelf/generic_plugin.html");
    webView->script(setPluginName.c_str());
}

bool Plugin::update()
{
    //char* data = new char(255);
    char data[255];
    data[0] = 0;
    fgets(data, 255, fp);
    if(data[0] != 0)
    {
        std::string consoleprint = std::string("consolePrint(\"")+std::string(data)+std::string("\")");
        webView->script(consoleprint);
        delete data;
    }
}

void Plugin::put(std::string pass)
{
    fputs(pass.c_str(), fp);
}
